/**
 * Created by leo on 5/31/16.
 */

var express = require('express');
var router = express.Router();
var Device = require('../models/device');

router.get('/', function(req, res) {
    Device.getDevices(function(err, device) {
        if (err) {
            throw err;
        }
        res.json(device);
    });
});

router.get('/:_id', function(req, res) {
    Device.getDeviceById(req.params._id, function(err, device) {
        if (err) {
            throw err;
        }
        res.json(device);
    });
});

router.post('/', function(req, res) {
    var device = req.body;
    Device.addDevice(device, function(err, device) {
        if (err) {
            throw err;
        }
        res.json(device);
    });
});

router.put('/:_id', function (req, res) {
    var id = req.params._id;
    var device = req.body;
    Device.updateDevice(id, device, {}, function(err, device) {
        if (err) {
            throw err;
        }
        res.json(device);
    });
});

router.delete('/:_id', function (req, res) {
    var id = req.params._id;
    Device.removeDevice(id, function(err, device) {
        if (err) {
            throw err;
        }
        res.json(device);
    });
});

module.exports = router;